package subpub

import "fmt"

type IEventLister interface {
	handel(eventName string)
}

type EventManager struct {
	listers map[string][]IEventLister
}

func (em *EventManager) AddLister(eventType string, lister IEventLister) {
	if v, ok := em.listers[eventType]; ok {
		em.listers[eventType] = append(v, lister)
	} else {
		v = make([]IEventLister, 1)
		em.listers[eventType] = append(v, lister)
	}
}

func (em *EventManager) Trigger(eventType string) {
	if lisenC, ok := em.listers[eventType]; ok {
		for _, v := range lisenC {
			v.handel(eventType)
		}
	}
}

type Editor struct {
	em *EventManager
}

func (e *Editor) Update() {
	fmt.Println("文件更新了")
	e.em.Trigger("文件更新事件")
}
